package com.qingxin.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.WellProduct;
import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.WellProductService;
import com.qingxin.park.vo.TraceCompany;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.web.service.TraceQueryService;


/**
 * 企业
 * @author Administrator
 *
 */
@Controller
public class QiyeAction extends BaseAction {
	
	@Autowired
	private CompanyService companyService;
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private WellProductService wellProductService;
	
	@Autowired
	private TraceQueryService traceQueryService;
	
	
	/**
	 * 企业
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/qiye.do", method=RequestMethod.GET)
	public String qiye(ModelMap model, HttpServletRequest request) {
		model.put("model", "qiye");
		return "cms/qiye";
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/c/listQiye.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listQiye(PageParam pageParam, HttpServletRequest request){
		pageParam.setPagesize(3);
		
		Pager pager = companyService.listPageCompanies(pageParam);
		List<Map<String, Object>> rslist = new ArrayList<>();
		List<Company> list = (List<Company>) pager.getList();
		if(list != null){
			for(Company company : list){
				Map<String, Object> companyMap = new HashMap<>();
				companyMap.put("companyid", company.getCompanyid());
				companyMap.put("companyName", company.getCompanyName());
				companyMap.put("imgurl", UploadFile.getFileUrl(UploadFile.DIR_COMPANY_IMG, company.getImgurl()));
				companyMap.put("remark", company.getRemark());
				
				List<FarmPark> parkList = farmParkService.listParks(company.getCompanyid());
				if(parkList != null && parkList.size()>0){
					FarmPark farmPark = parkList.get(0);
					Map<String, Object> parkMap = new HashMap<>();
					parkMap.put("parkid", farmPark.getParkid());
					parkMap.put("parkName", farmPark.getParkName());
					parkMap.put("logoUrl", UploadFile.getFileUrl(UploadFile.DIR_FARM_IMG, farmPark.getLogoUrl()));
					parkMap.put("remark", farmPark.getRemark());
					
					List<WellProduct> products = wellProductService.listParkWellProducts(farmPark.getParkid());
					if(products != null && products.size()>0){
						WellProduct product = products.get(0);
						Map<String, Object> productsMap = new HashMap<>();
						productsMap.put("productid", product.getProductid());
						productsMap.put("productName", product.getProductName());
						productsMap.put("productImg", UploadFile.getFileUrl(UploadFile.DIR_WELLPRODUCT_IMG, product.getProductImg()));
						
						parkMap.put("wellProduct1", productsMap);
					}
					if(products != null && products.size()>1){
						WellProduct product = products.get(1);
						Map<String, Object> productsMap = new HashMap<>();
						productsMap.put("productid", product.getProductid());
						productsMap.put("productName", product.getProductName());
						productsMap.put("productImg", UploadFile.getFileUrl(UploadFile.DIR_WELLPRODUCT_IMG, product.getProductImg()));
						
						parkMap.put("wellProduct2", productsMap);
					}
					
					companyMap.put("farmPark", parkMap);
					
				}
				
				rslist.add(companyMap);
			}
		}
		
		pager.setRows(rslist);
		
		return pager;
	}
	
	
	@RequestMapping(value="/c/qiyeDetail.do", method=RequestMethod.GET)
	public String qiyeDetail(ModelMap model, String id, HttpServletRequest request){
		model.put("model", "qiye");
		TraceCompany company = traceQueryService.getTraceCompany(id);
		model.put("company", company);
		
		return "cms/qiye_detail";
	}
	
	
}
